package com.miyu.book.utils.bookUtils;

import com.alibaba.fastjson.JSONObject;
import com.miyu.book.utils.web.PropertiesUtil;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * 书籍工具类
 * 提供书籍相关工具类使用
 */

public class BookCommutils {
    /**
     * uuid生成
     * @return
     */
    public static   String getUUID(){
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        return uuid;
    }

    /**
     * 根据字符转换MD5可校验数据时候重复
     * @param inStr
     * @return
     */
    public static String MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * 字符串转换流
     * @param sInputString
     * @return
     */
    public static InputStream getStrToStream(String sInputString) {
        if (sInputString !=null && !sInputString.trim().equals("")) {
            try {
                ByteArrayInputStream tInputStringStream =new ByteArrayInputStream(sInputString.getBytes());
                return tInputStringStream;
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 将一个输入流转化为字符串
     *
     * @param tInputStream
     * @return
     */
    public static String getStreamToStr(InputStream tInputStream) {
        if (tInputStream !=null) {
            try {
                BufferedReader tBufferedReader =new BufferedReader(new InputStreamReader(tInputStream));
                StringBuffer tStringBuffer =new StringBuffer();
                String sTempOneLine;
                while ((sTempOneLine = tBufferedReader.readLine()) !=null) {
                    tStringBuffer.append(sTempOneLine);
                }
                return tStringBuffer.toString();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 分表处理章节数据
     * 由于章节过多分10张表处理 增加查询效率
     * @return
     */
    public static String [] chTable(){
        String [] rs = new String[10];
        rs[0] = "a";
        rs[1] = "b";
        rs[2] = "c";
        rs[3] = "d";
        rs[4] = "e";
        rs[5] = "f";
        rs[6] = "g";
        rs[7] = "h";
        rs[8] = "i";
        rs[9] = "j";
        return  rs;
    }
    public static void main(String[] args) {
        int s = chTable().length;
        String [] strings = chTable();
        Random rd=new Random();
        System.out.println(strings[rd.nextInt(s)] );
    }

    /**
     * 随机获得一张表
     * @return
     */
    public static String getTable (){
        String [] rds = chTable();
        Random rd=new Random();
        return rds[rd.nextInt(rds.length)];
    }


    /**
     * 规则查询字符
     */
    public static String bookRuleStr(JSONObject param){
        //取出规则
        StringBuffer sql = new StringBuffer();
        sql.append("select rule_id ruleId ,rule_name ruleName ,root_source rootSource,root_search rootSearch,root_type rootType,rule_book_list ruleBookList,");
        sql.append(" rule_chapter_list ruleChapterList, rule_set ruleSet ");
        sql.append(" from book_rule_set where is_stop = '0' ");
        if(param.containsKey("ruleId"))
            sql.append(" and rule_id = '").append(param.getString("ruleId")).append("'");
        return  sql.toString();
    }

    /**
     * 判断当前数据库连接类型
     */
    public static String baseDataConnectionType(){
        String str = "sql_server";

        String driver =  PropertiesUtil.getProperty("spring.jpa.database");
        if(driver.indexOf("mysql")>-1)
            return "mysql";

        return str;
    }

    /**
     * 判断数据库连接类型 mysql 或者smmsql
     * @return
     */
    public static boolean IsMysql(){
        if(baseDataConnectionType().indexOf("sql_server")>-1)
            return  false;
        return true;
    }
}

